package cc.z8g.browser.core.tab

import android.app.Activity
import android.graphics.Bitmap
import android.webkit.WebView
import cc.z8g.browser.BrowserApp
import cc.z8g.browser.R
import java.util.concurrent.Callable

/**
 * Constructs a [TabModel].
 */
class TabFactory(
    private val activity: Activity,
    private val incognitoMode: Boolean,
    private val iconFreeze: Bitmap,
    private val tabWebChromeClientProvider: Callable<TabWebChromeClient>
) {

    /**
     * Constructs a tab from the [webView] with the provided [tabCreator].
     */
    fun constructTab(tabCreator: TabCreator, webView: WebView): TabModel {
        val headers = WebViewFactory.createRequestHeaders()
        return TabAdapter(
            tabCreator = tabCreator,
            webView = webView,
            requestHeaders = headers,
            tabWebViewClient = TabWebViewClient(UrlHandler(activity, incognitoMode), headers),
            tabWebChromeClient = tabWebChromeClientProvider.call(),
            defaultTabTitle = BrowserApp.getApplication().getString(R.string.text_size_untitled),
            iconFreeze = iconFreeze
        )
    }
}
